﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;

namespace DotNet.Utilities.Sms
{
    public class WeiKe
    {
        public static string ApiKey { get { return "c55fb8d42571b65cdcc1cd8a1b2ecf08"; } }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns>
        /// 调用成功的返回值示例：
        ///    {
        ///        "code":0,
        ///        "msg":"OK",
        ///        "user":{
        ///            "nick":"Jacky",
        ///            "gmt_created":"2012-09-11 15:14:00",
        ///            "mobile":"15268109538",
        ///            "email":"jacky@taovip.com",
        ///            "apikey":"9b11127a9701975c734b8aee81ee3526",
        ///            "ip_whitelist":null,		//IP白名单，推荐使用
        ///            "api_version":"v1",		//api版本号
        ///            "send_limit":10000,		//每天短信发送量限制，默认为1万
        ///             "send_count":0	,		//当天已发送的短信数
        ///             "balance ":0			//短信剩余条数
        ///        }
        ///    }
        /// </returns>
        public static JObject GetUserInfo()
        {
            HttpHelper Helper = new HttpHelper();
            HttpResult Result = Helper.GetHtml(new HttpItem()
            {
                URL = string.Format("http://api.taovip.com/v1/user/get.json?apikey={0}", ApiKey),
                Encoding = Encoding.UTF8
            });
            return JObject.Parse(Result.Html);
        }

        /// <summary>
        /// 获取余额
        /// </summary>
        /// <returns></returns>
        public static int GetBalance()
        {
            HttpHelper Helper = new HttpHelper();
            HttpResult Result = Helper.GetHtml(new HttpItem()
            {
                URL = string.Format("http://api.taovip.com/v1/user/get.json?apikey={0}", ApiKey),
                Encoding = Encoding.UTF8
            });
            return JObject.Parse(Result.Html)["user"]["balance"].Value<int>();
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="Mobile"></param>
        /// <param name="Text"></param>
        /// <returns>
        /// 0：执行成功
        /// 1：请求参数缺失
        /// 2：请求参数格式错误
        /// 3：账户余额不足
        /// -1：非法的apikey
        /// -2：API没有权限
        /// -3：IP没有权限
        /// -4：访问次数超限
        /// -5：访问频率超限
        /// </returns>
        public static int Send(string Mobile, string Text, string Company)
        {
            HttpHelper Helper = new HttpHelper();
            HttpResult Result = Helper.GetHtml(new HttpItem()
            {
                URL = "http://api.taovip.com/v1/sms/send.json",
                Method = "POST",
                Postdata = string.Format("apikey={0}&mobile={1}&text={2}", ApiKey, Mobile, System.Web.HttpUtility.UrlEncode(Text + "【" + Company + "】", Encoding.UTF8)),
                Encoding = Encoding.UTF8,
                ContentType = "application/x-www-form-urlencoded"
            });
            JObject R = JObject.Parse(Result.Html);
            return R["code"].Value<int>();
        }
    }
}
